// models/user.js
const connection = require('../database');

const User = {
    find: (callback) => {
        const query = 'SELECT * FROM users'
        connection.query(query, [], callback)
    },
    create: (userData, callback) => {
        const query = `INSERT INTO users (username, password, role) VALUES (?, ?, ?)`;
        connection.query(query, [userData.username, userData.password, userData.role], callback);
    },
    delete: (userId, callback) => {
        const query = `DELETE FROM users WHERE id = ?`;
        connection.query(query, [userId], callback);
    },
    update: (userId, userData, callback) => {
        const query = `UPDATE users SET username = ?, password = ?, role = ? WHERE id = ?`;
        connection.query(
            query,
            [userData.username, userData.password, userData.role, userId],
            callback
        );
    },
    login: ({ username, password }, callback) => {
        const query = `SELECT * FROM users WHERE username = ? and password = ?`;
        connection.query(query, [username, password], callback);
    },
    findAll: (page, limit, callback) => {
        // page是当前页码，limit是每页显示的数量
        // 计算OFFSET值
        const offset = (page - 1) * limit;
        const query = `
      SELECT * FROM users
      LIMIT ?
      OFFSET ?
    `;
        connection.query(query, [parseInt(limit), parseInt(offset)], callback);
    },
    // 可以根据需要添加更多的用户相关操作
};

module.exports = User;